<?php 
/**
 * PHP versions 4 and 5
 * MagicPHP
 * Licensed under The Open Group Test Suite License
 * Redistributions of files must retain the above copyright notice.
 * @copyright     Copyright 2011
 * @link          
 * @package       Model
 * @since         MaginPHP 1.0
 * @license     
 */
?>
<?php
	/**
	 * @desc: This Function Is Used To Create DropDown 
	 * @author: aNKIT kHAMBHATA
	 */
	class Magic_DropDown_Model {
		private static function queryForDropDown($table,$condition,$value,$displayvalue){
									$sql="select $value , $displayvalue from $table where $condition ";
									return $sql;				
								} 
		/**
		 * @author: aNKIT kHAMBHATA
		 * @desc: create dynamic dropdown box from existing table 
		 * @param $name: name of dropdown
		 * @param $id: name of id attribute 
		 * @param $class: class name attribute  
		 * @param $select_status: 1/0 .
		 *  1 = Used on the time of Update select value what insert before
		 * @param $select_id: value of from the drop down on time of insetion
		 * @param $select_compare_id: name of field which comes into dropdown
		 * @param $value: name of filed which value comes into value attribute 
		 * @param $display_value: name of filed which value comes into display name in dorpdown
		 * @param $event: name of event ex: onchange ,onselect etc ..........
		 * @param $function: name of javascript function which fire on event 
		 * @param $table: name of table
		 * @param $condition : condition
                 * array = ("name",
                 *          "id",
                 *          "select_status",
                 *          "select_id",
                 *          "select_compare_id",
                 *          "$value",
                 *          "display_value,
                 *          "$event",
                 *          "function",
                 *          "table",
                 *          "condition" );
		 */
		public function dropdown($name,$id,$class,$select_status,$select_id,$select_compare_id,$value,$display_value,$event,$function,$table,$condition) {
							$sql = self :: queryForDropDown($table, $condition, $value,$display_value);
							if($event != "") {
								$substr=$event.'="'.$function.'"';
							} else {
								$substr="";
							}
							$str='<select name="'.$name.'" width="50" id="'.$id.'" class="'.$class.'"  '.$substr.'>
							<option value="" selected="selected">Select</option>';
							$res=mysql_query($sql);
							while($row=@mysql_fetch_assoc($res)) {
								if($select_status=='0') {
									$str.='<option value='.$row[$value].'>'.$row[$display_value].'</option>';
								} else {
									if($select_id == $row[$select_compare_id]) {
										$str.='<option value='.$row[$value].' selected="selected">'.$row[$display_value].'</option>';
									}	else  {
                                        $str.='<option value='.$row[$value].'>'.$row[$display_value].'</option>';
                                    }
								}	
							}
							$str.="</select>";
							return $str;
						}
		/**
		 * @author: aNKIT kHAMBHATA
		 * @desc: create dynamic dropdown box from existing table 
		 * @param $name: name of dropdown
		 * @param $id: name of id 
		 * @param $class: name of class 
		 * @param $select_status: 1/0
		 * @param $select_id: if 1 than value of dropdown on update time 
		 * @param $value: name of value field 
		 * @param $display_value: name of display name 
		 * @param $event: name of event ex: onchange /onselect etc 
		 * @param $function: name of javascript function which fire on event
		 * @param $table : Name Of Table
		 * @param $condition : where condition 
		 * @version: 1.2
		 */
		public function multipleDropdown($name,$id,$class,$select_status,$select_id,$value,$display_value,$event,$function,$table,$condition){
							$sql=self :: queryForDropDown($table,$condition,$value, $display_value);
							$str='<select name="'.$name.'" multiple="multiple" size="10"  id="'.$id.'" class="'.$class.'"  '.$event.'="'.$function.'">
							<option value="" selected="selected">Select</option>';
							$i=0;	
							$ids=explode(",",$select_id);
							//print_r($ids);
							$res=mysql_query($sql);
							while($row=@mysql_fetch_assoc($res)) {
								if($select_status=='0') {
									$str.='<option value='.$row[$value].'>'.$row[$display_value].'</option>';
								} else if($select_status=='1')  {
									$key = array_search($row[$value], $ids);
								    if($key > -1 ) {
                                		$str.='<option value='.$row[$value].' selected="selected">'.$row[$display_value].'</option>';
									}	else  {
										$str.='<option value='.$row[$value].'>'.$row[$display_value].'</option>';
									}
								}
								$i++;	
							}
							$str.="</select>";
							return $str;
					}
       	public function dropdownWithoutSql($name,$id,$class,$selectstatus,$valuearray,$displayvaluearray,$selectvalue,$event,$function) {
                                    if($event != "") {
                                        $substr=$event.'="'.$function.'"';
                                    } else {
                                        $substr="";
                                    }
                                    $str='<select name="'.$name.'" width="50" id="'.$id.'" class="'.$class.'"  '.$substr.'>
                                            <option value="" selected="selected">Select</option>';
                                    for($i=0;$i<count($valuearray);$i++){
                                        if($selectstatus == '1') {
                                            if($selectvalue == $valuearray[$i]){
                                                $str.='<option value='.$valuearray[$i].' selected="selected">'.$displayvaluearray[$i].'</option>';
                                            } else {
                                                $str.='<option value='.$valuearray[$i].'>'.$displayvaluearray[$i].'</option>';
                                            }
                                        } else {
                                            $str.='<option value='.$valuearray[$i].'>'.$displayvaluearray[$i].'</option>';
                                        }
                                    }
                                    $str .= '</select>';
                                    return $str ;
       	}

       	public function mulipleDropdownWithoutSql($name,$id,$class,$selectstatus,$valuearray,$displayvaluearray,$selectvalue,$event,$function) {
				       		if($event != "") {
				       			$substr=$event.'="'.$function.'"';
				       		} else {
				       			$substr="";
				       		}
				       		$ids=explode(",",$selectvalue);
				       		 
				       		$str='<select name="'.$name.'" width="50" id="'.$id.'" multiple="multiple" size="10"  class="'.$class.'"  '.$substr.'>
				                                                <option value="" >----Select----</option>';
				       		for($i=0;$i<count($valuearray);$i++){
				       			if($selectstatus == '1') {
				       				if(in_array($valuearray[$i],$ids)) {
				       					$str.='<option value='.$valuearray[$i].' selected="selected">'.$displayvaluearray[$i].'</option>';
				       				} else {
				       					$str.='<option value='.$valuearray[$i].'>'.$displayvaluearray[$i].'</option>';
				       				}
				       			} else {
				       				$str.='<option value='.$valuearray[$i].'>'.$displayvaluearray[$i].'</option>';
				             	}
				             }
				             $str .= '</select>';
				             return $str ;
						}

		} // End Of Class 
?>